package drds.configuration.db.xml_mapping.dynamic_tag.token_handler;

import drds.configuration.db.xml_mapping.configuration.Configuration;
import lombok.NonNull;

import java.util.List;

public class PreparedParameterMappingTokenHandler implements TokenHandler {
    public Configuration configuration;
    private List<PreparedParameterMapping> preparedParameterMappingList;

    public PreparedParameterMappingTokenHandler(Configuration configuration, List<PreparedParameterMapping> preparedParameterMappingList) {
        this.configuration = configuration;
        this.preparedParameterMappingList = preparedParameterMappingList;
    }

    @Override
    public String handleToken(String content) {
        preparedParameterMappingList.add(parseParameterMapping(content));
        return "?";
    }

    private PreparedParameterMapping parseParameterMapping(@NonNull String content) {
        content = content.trim();
        PreparedParameterMapping preparedParameterMapping = new PreparedParameterMapping();
        if (
                content.contains(".")
        ) {
            String alias = content.split("\\.")[0];
            String name = content.split("\\.")[1];

            preparedParameterMapping.setColumnPrefix(alias);
            preparedParameterMapping.setColumnName(name);
        } else {
            String name = content;
            preparedParameterMapping.setColumnName(name);
        }
        return preparedParameterMapping;
    }
}
